package com.tanhua.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;

import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    /**
     * 保存信息
     *
     * @param content
     */
    public void saveQuestion(String content) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //获取问题
        Question question = questionApi.findByUserid(userId);
        if (question == null) {
            //如果不存在，保存信息
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //如果已经存在，更新信息
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    /**
     * 用户通用设置 - 读取
     *
     * @return
     */
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1、获取用户id
        //从线程中获取id
        Long userId = UserHolder.getUserId();
        //2、获取用户的手机号码
        String mobile = UserHolder.get().getMobile();
        //3、获取用户的陌生人问题
        Question question = questionApi.findByUserid(userId);
        String txt = question == null ? "你喜欢摄影吗？" : question.getTxt();
        //将vo添加问题数据、用户id、手机号
        vo.setStrangerQuestion(txt);
        vo.setId(userId);
        vo.setPhone(mobile);
        //4、获取用户的APP通知开关数据
        Settings settings = settingsApi.findByUserid(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;
    }

    /**
     * 通知设置-修改保存
     */
    public void settingsUpdate(Map map) {
        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        //从线程中获取id
        Long userId = UserHolder.getUserId();
        //4、获取用户的APP通知开关数据
        Settings settings = settingsApi.findByUserid(userId);
        if (settings == null) {
            //如果不存在，就新建一个设置对象并保存
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settingsApi.save(settings);
        } else {
            //如果已经存在就直接修改设置
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settingsApi.update(settings);
        }
    }

    /**
     * 查询黑名单列表
     */
    public PageResult blacklist(int page, int pagesize) {
        //从线程中获取用户id
        Long userId = UserHolder.getUserId();
        //通过黑名单api查找被拉黑的用户的集合
        Page<UserInfo> infoPage = blackListApi.backlist(userId,page,pagesize);
        //将查询的信息封装到PageResult对象中
//        PageResult result = new PageResult(page,pagesize, (int) infoPage.getTotal(),infoPage.getRecords());
        PageResult result = new PageResult();
        result.setPage(page);
        result.setPagesize(pagesize);
        result.setCounts((int) infoPage.getTotal());
        result.setItems(infoPage.getRecords());
        //返回对象
        return result;
    }

    /**
     * 黑名单删除
     * @param uid
     */
    public void blackByIdDelete(Long uid) {
        //从线程中获取用户id
        Long userId = UserHolder.getUserId();
        //用过用户id删除指定的黑名单id
        blackListApi.blackByIdDelete(userId,uid);
    }
}
